<template>
  <!-- 员工管理页 -->
  <div class="waiter">
    <!-- 头部区域 -->
    <div class="header">
      <el-button type="primary" plain @click="addWaiter">添加</el-button>
      <el-input v-model="waiterName" style="width: 240px" placeholder="请输入用户名" clearable />
      <el-select v-model="WaiterStatus" clearable placeholder="请选择状态">
        <el-option label="正常" value="正常" />
        <el-option label="禁用" value="禁用" />
      </el-select>
      <el-button type="primary" plain @click="queryWaiter()">查询</el-button>
    </div>
    <!-- 表格内容区域 -->
    <div class="table_content">
      <el-table :data="WaiterData" style="width: 100%">
        <!-- 编号 -->
        <el-table-column prop="id" label="编号" align="center" />
        <!-- 用户名 -->
        <el-table-column prop="username" label="用户名" align="center" />
        <!-- 姓名 -->
        <el-table-column prop="realname" label="姓名" align="center" />
        <!-- 手机号 -->
        <el-table-column prop="telephone" label="手机号" align="center" />
        <!-- 性别 -->
        <el-table-column prop="gender" label="性别" align="center">
          <template slot-scope="scope">
            <i v-if="scope.row.gender==='male'" class="el-icon-male green_info" />
            <i v-else class="el-icon-female red_info" />
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="scope.row.status === '正常' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button style="color:#2cb5ac" size="mini" @click="editWaiter(scope.row)">编辑</el-button>
            <el-button
              style="color:red"
              size="mini"
              @click="deleteWaiter(scope.row.id)"
            >删除</el-button>
            <el-button
              type="info"
              size="mini"
              @click="waiterDetails(scope.row.id)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部页面切换区域 -->
    <div class="pagination">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[3,6,9,12,15,]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 模态框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <!-- 表单区域 -->
      <!-- :model 表单对象 最终我们要提交的那个表单对象 -->
      <!-- :rules 表单验证的规则 -->
      <!-- ref 类比与 id 最终通过ref找到这张表单 -->
      <el-form
        ref="waiter_form"
        :model="waiterForm"
        :rules="rules"
        label-width="80px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="waiterForm.username" />
        </el-form-item>
        <!-- 姓名 -->
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="waiterForm.realname" />
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model.number="waiterForm.telephone" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="waiterForm.password" type="password" />
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="waiterForm.gender">
            <el-radio label="male" value="male">男</el-radio>
            <el-radio label="female" value="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="dialogTitle.indexOf('添加') < 0"
          label="状态"
          label-width="80px"
          prop="status"
        >
          <el-radio-group v-model="waiterForm.status">
            <el-radio label="正常" value="正常" />
            <el-radio label="禁用" value="禁用" />
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span
        v-if="dialogTitle.indexOf('添加') >= 0"
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary" @click="submitFormadd">确 定</el-button>
      </span>
      <span v-else slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary" @click="submitFormupdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        telephone: [{ required: true, message: '请输入号码', trigger: 'blur' },
        { type: 'number', message: '手机必须为数字值'}],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      },
      // 表单对象
      waiterForm: {
        username: '',
        password: '',
        telephone: '',
        realname: '',
        gender: ''
      },
      dialogVisible: false,
      // 模态框标题
      dialogTitle: '添加用户信息',
      // 员工数组
      WaiterData: [],
      //
      pageSize: 9,
      total: 10,
      // 当前页
      currentPage: 1,
      // 用户名
      waiterName: '',
      // 员工状态
      WaiterStatus: '',
      queryData: {}
    }
  },
  computed: {},
  created() {
    this.getAllWaiterData()
  },
  mounted() {},
  methods: {
    waiterDetails(id) {
      this.$router.push({
        path: '/admin/waiter/Details',
        query: {
          id: id
        }
      })
    },
    // 更新
    submitFormupdate() {
      this.$refs['waiter_form'].validate(async(valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          const res = await post('/baseUser/saveOrUpdate', this.waiterForm)
          // 给用户一个成功的提示
          if (res.status === 200) {
            this.$message({
              message: res.message,
              type: 'success'
            })
            // 重新刷新页面的数据
            this.getAllWaiterData()
            // 调用关闭模态框的方法
            this.dialogClose()
          } else {
            this.$message.error(res.message)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 添加
    submitFormadd() {
      this.$refs['waiter_form'].validate(async(valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          const res = await post('/user/registerEmployee', this.waiterForm)
          // 给用户一个成功的提示
          if (res.status === 200) {
            this.$message({
              message: '添加成功！',
              type: 'success'
            })
            // 重新刷新页面的数据
            this.getAllWaiterData()
            // 调用关闭模态框的方法
            this.dialogClose()
          } else {
            this.$message.error(res.message)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭模态框 调用表单重置的方法
    dialogClose() {
      // 重置表单
      this.$refs['waiter_form'].resetFields()
      // 关闭模态框
      setTimeout(() => {
        this.dialogVisible = false
      }, 200)
    },
    // 编辑员工信息
    editWaiter(row) {
      this.waiterForm = { ...row }
      this.waiterForm.telephone = row.telephone - 0
      this.dialogTitle = '编辑员工信息'
      // 显示模态框
      this.dialogVisible = true
    },
    // 添加员工信息
    addWaiter() {
      this.waiterForm = {}
      this.dialogTitle = '添加员工信息'
      // 显示模态框
      this.dialogVisible = true
    },
    // 查询
    queryWaiter() {
      const data = {
        username: this.waiterName,
        status: this.WaiterStatus
      }
      // 格式化参数
      for (const key in data) {
        if (!data[key]) {
          delete data[key]
        }
      }
      console.log(data)
      this.queryData = data
      this.currentPage = 1
      this.getAllWaiterData()
    },
    // 删除
    async deleteWaiter(id) {
      const res = await get('/baseUser/deleteById', { id })
      // console.log(res);
      if (res.status === 200) {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.getAllWaiterData()
      } else {
        this.$message.error(res.message)
      }
    },
    // 分页获取所有员工信息
    async getAllWaiterData() {
      const data = {
        page: this.currentPage,
        pageSize: this.pageSize,
        roleId: 1
      }
      // console.log({ ...data, ...this.queryData });
      // 通过对应的参数发送网络请求
      const res = await get('/baseUser/pageQuery', {
        ...data,
        ...this.queryData
      })
      this.WaiterData = res.data.list
      this.total = res.data.total
      // console.log(res);
      // console.log(res.data.list);
    },
    // 分页查询
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getAllWaiterData()
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // val
      this.currentPage = val
      // 重新调用
      this.getAllWaiterData()
      // console.log(`当前页: ${val}`);
    }
  }
}
</script>
<style scoped>
.head {
  line-height: 38px;
}
.pagination {
  position: fixed;
  bottom: 25px;
  right: 10px;
}
</style>
